const { Router } = require('express')
const express = require('express')
//路由容器
const userComment = express.Router()
const axios = require('axios')
const { render } = require('art-template')
const multipart = require('connect-multiparty');
const FormData = require('form-data')
var multipartMiddleware = multipart()
const fs = require("fs");
// 用户评论的接口
userComment.post('/commentGoods', multipartMiddleware, (req, res) => {
    const {GComment, goodsId , shopId, userId} = req.body
    
    console.log(req.files)
    let formData = new FormData()
     for(var a in req.files){  
                  //接收前台传来的图片路径
        formData.append('files', fs.createReadStream(req.files[a].path))
    }
    formData.append('userId', userId)
    formData.append('shopId', shopId)
    formData.append('goodsId', goodsId)
    formData.append('GComment', GComment)
    // axios({
    //     url:`http://www.mianded.top/commentGoods?userId=${userId}&shopId=${shopId}&goodsId=${goodsId}&GComment=${GComment}`,
    //     method:'POST',
    //     data: formData,
    //     headers:{
    //         'Content-Type':"multipart/form-data",
    //         // token:req.session.token?req.session.token:"",
    //     }
    // })
    // .then(data => {
    //     console.log(data.data);
    //     res.send(data)
    // })
    // .catch(err => {
    //     // console.log(err);
    //     res.send(err)
    // })
    axios.post(`http://www.mianded.top:8081/comment/commentGoods`, formData, { headers: formData.getHeaders() }).then((data) => {
        // console.log(data)
        console.log(333333333);
        console.log("success============")
        res.send({ data: data.data })
    }).catch((err) => {
        res.send(err)
    })
})



module.exports = userComment